<?php
defined('YII_ENV') or exit('Access Denied');
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/6/19
 * Time: 16:52
 */
$urlManager = Yii::$app->urlManager;
$this->title = '专题编辑';
$this->params['active_nav_group'] = 8;
?>
<style>
    .goods-item,
    .video-item {
        margin: 1rem 0;
    }

    .goods-item .goods-name,
    .video-item .video-name {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
</style>
<div class="panel mb-3" id="app">
    <div class="panel-header"><?= $this->title ?></div>
    <div class="panel-body">
        <form class="auto-form" method="post" return="<?= $urlManager->createUrl(['mch/dynamic/index']) ?>">
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label required">标题</label>
                </div>
                <div class="col-sm-6">
                    <input class="form-control" name="title" value="<?= str_replace("\"", "&quot", $model->title) ?>">
                </div>
            </div>
            <div class="form-group row" >
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">简介</label>
                </div>
                <div class="col-sm-6">
                    <textarea class="form-control" name="sub_title" ><?= str_replace("\"", "&quot", $model->sub_title) ?></textarea>
                </div>
            </div>
			
			
   
            <div class="form-group row">
				<div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">图片组</label>
                </div>
				<div class="col-sm-6">
				
				   <?php if ($model['pic_list']) :
								
									$pic_list = unserialize($model['pic_list']);
								  
								else :
									$pic_list = [];
								endif; ?>

							

					<div class="upload-group multiple short-row">
						<div class="input-group">
							<input class="form-control file-input" readonly>
							<span class="input-group-btn">
				<a class="btn btn-secondary upload-file" href="javascript:"
				   data-toggle="tooltip"
				   data-placement="bottom" title="上传文件">
					<span class="iconfont icon-cloudupload"></span>
				</a>
			</span>
							<span class="input-group-btn">
				<a class="btn btn-secondary select-file" href="javascript:"
				   data-toggle="tooltip"
				   data-placement="bottom" title="从文件库选择">
					<span class="iconfont icon-viewmodule"></span>
				</a>
			</span>
						</div>
						<div class="upload-preview-list" id="sortList">
							<?php if (count($pic_list) > 0) : ?>
								<?php foreach ($pic_list as $item) : ?>
									<div class="upload-preview text-center" flex="cross:center">
										<input type="hidden" class="file-item-input"
											   name="pic_list[]"
											   value="<?= $item ?>">
										<span class="file-item-delete">&times;</span>
										<span class="upload-preview-tip">750&times;750</span>
										<img class="upload-preview-img" src="<?= $item ?>">
									</div>
								<?php endforeach; ?>
							<?php else : ?>
								<div class="upload-preview text-center">
									<input type="hidden" class="file-item-input"
										   name="pic_list[]">
									<span class="file-item-delete">&times;</span>
									<span class="upload-preview-tip">750&times;750</span>
									<img class="upload-preview-img" src="">
								</div>
							<?php endif; ?>
						</div>
					</div>

				</div>
			</div>

            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">是否禁用</label>
                </div>
                 <div class="col-sm-6">
                    <label class="radio-label">
                        <input value="0" <?= $model->is_chosen == 0 ? 'checked' : null ?> name="is_chosen" type="radio"
                               class="custom-control-input">
                        <span class="label-icon"></span>
                        <span class="label-text">不</span>
                    </label>
                    <label class="radio-label">
                        <input value="1" <?= $model->is_chosen == 1 ? 'checked' : null ?> name="is_chosen" type="radio"
                               class="custom-control-input">
                        <span class="label-icon"></span> 
                        <span class="label-text">禁用</span>
                    </label>
                </div>
            </div>
			 <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">url链接</label>
                </div>
                <div class="col-sm-6">
                    <input class="form-control" name="url" value="<?= $model->url ?>">
                    <div class="text-muted fs-sm">url链接</div>
                </div>
            </div>
			 <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">商品id</label>
                </div>
                <div class="col-sm-6">
                    <input class="form-control" type="number" name="goods_id" value="<?= $model->goods_id ?>">
                    <div class="text-muted fs-sm">商品id</div>
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">排序</label>
                </div>
                <div class="col-sm-6">
                    <input class="form-control" name="sort" value="<?= $model->sort ?>">
                    <div class="text-muted fs-sm">升序，数字越小排序越靠前，默认1000</div>
                </div>
            </div>
          

            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                </div>
                <div class="col-sm-6">
                    <a class="btn btn-primary auto-form-btn" href="javascript:">保存</a>
                </div>
            </div>
        </form>

        <!-- Modal -->
        
</div>

<script src="<?= Yii::$app->request->baseUrl ?>/statics/ueditor/ueditor.config.js?v=1.6.2"></script>
<script src="<?= Yii::$app->request->baseUrl ?>/statics/ueditor/ueditor.all.min.js"></script>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            goods_list: null,
            video_list: null,
        },
    });
    var ue = UE.getEditor('editor', {
        serverUrl: "<?=$urlManager->createUrl(['upload/ue'])?>",
        enableAutoSave: false,
        saveInterval: 1000 * 3600,
        enableContextMenu: false,
        autoHeightEnabled: false,
    });

    $(document).on("submit", ".search-goods-form", function () {
        var form = $(this);
        var btn = form.find(".btn");
        btn.btnLoading("正在搜索");
        $.ajax({
            url: form.attr("action"),
            dataType: "json",
            data: form.serialize(),
            success: function (res) {
                btn.btnReset();
                if (res.code == 0) {
                    app.goods_list = res.data.list;
                }
            }
        });
        return false;
    });

    $(document).on("click", ".insert-goods", function () {
        var index = $(this).attr("index");
        var goods = app.goods_list[index];
        var _html = '';
        _html += '<br><div>';
        _html += '<a class="goods-link" goods="true" href="/pages/goods/goods?id=' + goods.id + '" style="display: block;background: #f3f3f3;border: 1px solid #eee;position: relative;height: 6rem;color: #333;text-decoration: none">';
        _html += '<img mode="aspectFill" class="goods-img" src="' + goods.cover_pic + '">';
        _html += '<div class="goods-info flex-col" style="padding:.5rem .5rem .5rem 6rem">';
        _html += '<div class="goods-name flex-grow-1">' + goods.name + '</div>';
        _html += '<div class="flex-grow-0">';
        _html += '<b class="goods-price" style="color:#ff4544">￥' + goods.price + '</b>';
        _html += '<span class="buy-btn" style="display: inline-block;float: right;font-size: 12px;border: 1px solid #ff4544;color: #ff4544;border-radius: .15rem;padding: .25rem .5rem;">去购买</span>';
        _html += '</div>';
        _html += '</div>';
        _html += '</a>';
        _html += '</div>';
        ue.execCommand("inserthtml", _html);
    });


    $(document).on("submit", ".search-video-form", function () {
        var form = $(this);
        var btn = form.find(".btn");
        btn.btnLoading("正在搜索");
        $.ajax({
            url: form.attr("action"),
            dataType: "json",
            data: form.serialize(),
            success: function (res) {
                btn.btnReset();
                if (res.code == 0) {
                    app.video_list = res.data.list;
                }
            }
        });
        return false;
    });

    $(document).on("click", ".insert-video", function () {
        var index = $(this).attr("index");
        var video = app.video_list[index];
        var _html = '';
        _html += '<video src="' + video.src + '"></video>';
        _html += '';
        _html += '';
        _html += '';
        _html += '';
        _html += '';
        _html += '';
        ue.execCommand("inserthtml", _html);
    });


</script>